//
//  SecuritySetViewController.m
//  Kjs-iOS
//
//  Created by cfq on 16/6/10.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "SecuritySetViewController.h"
#import "SecuritySetFunctionCell.h"
#import "SetLoginPswViewController.h"
#import "SetTradePswViewController.h"
#import "GesturePswViewController.h"
#import "BindTelViewController.h"
#import "BankCardViewController.h"
#import "RealAuthViewController.h"
#import "AppDelegate.h"
#import "KJSRequest.h"
#import "PRealAuthResultViewController.h"
#import "ORealAuthResultViewController.h"
#import "ReSetTradePswViewController.h"
#import "SecuritySetFunctionForOrgCell.h"
#import "AuthDetalViewController.h"
#import "OrgAuthDetailViewController.h"

@interface SecuritySetViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) PRealAuth *pRealAuthInfo;
@property (nonatomic,strong) ORealAuth *oRealAuthInfo;
@end

@implementation SecuritySetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"安全设置"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (CurentDelegate.account) {
        if ([CurentDelegate.account.type isEqualToString:@"p"]) {
            [self GetPRealInfo];
        }else if ([CurentDelegate.account.type isEqualToString:@"o"]){
            [self GetORealInfo];
        }
    }
}
#pragma mark - custom method
-(void)GetPRealInfo
{
    [KJSRequest GetPRealAuthInfoWithPartyId:CurentDelegate.account.partyId ret:^(PRealAuthResultModel *model) {
        self.pRealAuthInfo = model.realAuthInfo;
        [self.tableView reloadData];
    }];
}
-(void)GetORealInfo
{
    [KJSRequest GetORealAuthInfoWithPartyId:CurentDelegate.account.partyId ret:^(ORealAuthResultModel *model) {
        self.oRealAuthInfo = model.realAuthInfo;
        [self.tableView reloadData];
    }];
}
#pragma mark - UITableViewSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([CurentDelegate.account.type isEqualToString:@"p"] ) {
        //个人用户
        return 5;
    }else if ([CurentDelegate.account.type isEqualToString:@"o"]){
        if([CurentDelegate.account.levels integerValue] >= 2){
            //机构用户已验证法人手机
            return 4;
        }else{
            //机构用户未验证法人手机
            return 5;
        }
    }else{
        return 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([CurentDelegate.account.type isEqualToString:@"p"]) {
        //个人用户
        SecuritySetFunctionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecuritySetFunctionCell"];
        cell.type = indexPath.row;
        return cell;
    }else if([CurentDelegate.account.type isEqualToString:@"o"]){
        //机构用户
        SecuritySetFunctionForOrgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecuritySetFunctionForOrgCell"];
        cell.type = indexPath.row;
        return cell;
    }else{
        //不是机构或个人，会隐藏最后一个cell，所以这里采用哪一个cell都可以
        SecuritySetFunctionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecuritySetFunctionCell"];
        cell.type = indexPath.row;
        return cell;
    }
    
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case SecurityRealAuth:
        {
            NSLog(@"实名认证");
            if ([CurentDelegate.account.type isEqualToString:@"o"]) {
                if (![self.oRealAuthInfo.auditingOpinion isEqualToString:@"0"]) {
                    ORealAuthResultViewController *oRealAuthResultVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"ORealAuthResultViewController"];
                    oRealAuthResultVC.realAuthInfo = self.oRealAuthInfo;
                    [self displayViewController:oRealAuthResultVC];
                }else{
                    //审核不通过
                    OrgAuthDetailViewController *realAuthVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"OrgAuthDetailViewController"];
                    realAuthVC.auditingOpinion = self.oRealAuthInfo.auditingOpinion;
                    [self displayViewController:realAuthVC];
                }
            }else if ([CurentDelegate.account.type isEqualToString:@"p"]){
                if (![self.pRealAuthInfo.auditingOpinion isEqualToString:@"0"]) {
                    PRealAuthResultViewController *pRealAuthResultVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"PRealAuthResultViewController"];
                    pRealAuthResultVC.pRealAuth = self.pRealAuthInfo;
                    [self displayViewController:pRealAuthResultVC];
                }else{
                    //审核不通过
                    AuthDetalViewController *realAuthVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"AuthDetalViewController"];
                    realAuthVC.auditingOpinion = self.pRealAuthInfo.auditingOpinion;
                    [self displayViewController:realAuthVC];
                }
            }else{
                RealAuthViewController *realAuthVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"RealAuthViewController"];
                [self displayViewController:realAuthVC];
            }
        }
            break;
        case SecurityTelBind:
        {
            NSLog(@"手机绑定");
            BindTelViewController *bindTelVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"BindTelViewController"];
            bindTelVC.bindSuccess = ^(){
                [self loginOut];
            };

            [self displayViewController:bindTelVC];
        }
            break;
        case SecurityLoginPsw:
        {
            NSLog(@"登录密码");
            SetLoginPswViewController *setLoginPswVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"SetLoginPswViewController"];
            [self displayViewController:setLoginPswVC];
            
        }
            break;
        case SecurityTradePsw:
        {
            if ([CurentDelegate.account.type isEqualToString:@"p"]) {
                if (![self.pRealAuthInfo.auditingOpinion isEqualToString:@"0"]) {
                    NSLog(@"交易密码");
                    if ([CurentDelegate.account.levels integerValue] > 1) {
                        //已设置过交易密码
                        SetTradePswViewController *setTradePswVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"SetTradePswViewController"];
                        [self displayViewController:setTradePswVC];
                    }else if ( [CurentDelegate.account.levels integerValue] == 1){
                        ReSetTradePswViewController *reSetTradePswVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"ReSetTradePswViewController"];
                        [self displayViewController:reSetTradePswVC];
                    }

                }else{
                    [GUATool showDialog:@"请先进行实名认证" message:nil waitingTime:1.5];
                }
            }else if ([CurentDelegate.account.type isEqualToString:@"o"]){
                if (![self.oRealAuthInfo.auditingOpinion isEqualToString:@"0"]) {
                    if ([CurentDelegate.account.levels integerValue] > 2) {
                        //已设置过交易密码
                        SetTradePswViewController *setTradePswVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"SetTradePswViewController"];
                        [self displayViewController:setTradePswVC];
                    }else if ( [CurentDelegate.account.levels integerValue] == 2){
                        ReSetTradePswViewController *reSetTradePswVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"ReSetTradePswViewController"];
                        [self displayViewController:reSetTradePswVC];
                    }else{
                        [GUATool showDialog:@"请先绑定法人手机号" message:nil waitingTime:1.5];
                    }
                }else{
                    [GUATool showDialog:@"请先进行实名认证" message:nil waitingTime:1.5];
                }
            }else{
                [GUATool showDialog:@"请先进行实名认证" message:nil waitingTime:1.5];
            }
            
        }
            break;
        case SecurityBankCardOrBindRepresentTel:
        {
            if ([CurentDelegate.account.type isEqualToString:@"p"]) {
                if (![self.pRealAuthInfo.auditingOpinion isEqualToString:@"0"]) {
                    NSLog(@"银行卡管理");
                    if ([CurentDelegate.account.levels integerValue] < 2){
                        [GUATool showDialog:@"请先设置交易密码" message:nil waitingTime:1.5];
                    }else{
                        BankCardViewController *bankCardVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"BankCardViewController"];
                        [self displayViewController:bankCardVC];
                    }
                }else{
                    [GUATool showDialog:@"请先进行实名认证" message:nil waitingTime:1.5];
                }
              
            }else if([CurentDelegate.account.type isEqualToString:@"o"]){
                if (![self.oRealAuthInfo.auditingOpinion isEqualToString:@"0"]) {
                    BindTelViewController *bindTelVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"BindTelViewController"];
                    bindTelVC.bindType = @"represent";
                    [self displayViewController:bindTelVC];
                }else{
                    [GUATool showDialog:@"请先进行实名认证" message:nil waitingTime:1.5];
                }
            }else{
                [GUATool showDialog:@"请先进行实名认证" message:nil waitingTime:1.5];
            }
            
            
        }
            break;
        case SecurityGesturePsw:
        {
            NSLog(@"手势密码");
            GesturePswViewController *gesturePswVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"GesturePswViewController"];
            [self displayViewController:gesturePswVC];
        }
            break;
        default:
            NSLog(@"跳转异常");
            break;
    }
    
    
}

- (void)loginOut
{
    NSLog(@"登出");
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@"unautoLogin" forKey:@"ifAutoLogin"];
    CurentDelegate.account = NULL;
    CurentDelegate.IMpassword = @"";
    CurentDelegate.IMuserID = @"";
    //当退出登录后，tabiew可以打开登录界面要重新加载
    CurentDelegate.ifReLoad = YES;
    CurentDelegate.IMpassword = @"";
    CurentDelegate.IMuserID = @"";
    if (self.loginOutSuccess) {
        self.loginOutSuccess();
    }
    [self dismiss:YES];
}
-(void)dismiss:(BOOL)animated
{
    if (self.refreshAccountIndex) {
        self.refreshAccountIndex();
    }
    [super dismiss:animated];
}
@end
